﻿using UnityEngine;
using UnityEngine.UI;

public class MusicTab : MonoBehaviour {
    public Button m_butClose;
    public GridLayoutGroup m_Grid;
    private void Awake()
    {
        int MusicCount = ReaderInfo.Scorelist.Count;
        for (int i = 0; i < MusicCount; i++)
        {
            GameObject obj = Instantiate(Resources.Load("UI/MusicBut")) as GameObject;
            obj.transform.SetParent(m_Grid.transform);
            obj.GetComponent<Button>().onClick.AddListener(delegate () { GlobalHelper.MainGame.Btn_Music(obj); });
            obj.name = i.ToString();
        }
        SetGridHeight(MusicCount);
        m_butClose.onClick.AddListener(delegate () { this.CloseTab(); });
        CloseTab();
    }
    void CloseTab()
    {
        gameObject.SetActive(false);
        GlobalHelper.IsTurn = true;
    }
    #region 设置Grid大小及位置
    public void SetGridHeight(int Count)     //每行Cell的个数  
    {
        int Column;
        if (0 == Count % 2)
        {
            Column = Count / 2;
        }
        else
        {
            Column = Count / 2 + 1;
        }
        float wide = (Column * m_Grid.cellSize.x) + ((Column - 1) * m_Grid.spacing.x) + 3.0f;//列数*宽度+间隔     3.0f是微调
        if (wide < 900)
        {
            wide = 900;
        }
        m_Grid.GetComponent<RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, wide);
        m_Grid.transform.localPosition = new Vector3(-1/2*wide+450f,0f, 0f);
    }
    #endregion
}
